package cn.org.timespace.easyframe.util.sms.telecom;

import java.io.UnsupportedEncodingException;

public class Util {

	/** 
	* UCS2���� 
	* @param src UCS2 Դ�� 
	* @return ������UTF-16BE�ַ� 
	* @throws Exception 
	*/
	public static String DecodeUCS2(String src) throws Exception
	{
	   byte[] bytes = new byte[src.length() / 2];
	   for (int i = 0; i < src.length(); i += 2)
	   {
	    bytes[i / 2] = (byte) (Integer.parseInt(src.substring(i, i + 2), 16));
	   }
	   String reValue;
	   try
	   {
	    reValue = new String(bytes, "UTF-16BE");
	   }
	   catch (UnsupportedEncodingException e)
	   {
	    throw new Exception(e);
	   }
	   return reValue;

	}

	/** 
	* UCS2���� 
	* @param src       UTF-16BE�����Դ�� 
	* @return ������UCS2�� 
	* @throws Exception 
	*/
	public static String EncodeUCS2(String src) throws Exception
	{

	   byte[] bytes;
	   try
	   {
	    bytes = src.getBytes("UTF-16BE");
	   }
	   catch (UnsupportedEncodingException e)
	   {
	    throw new Exception(e);
	   }

	   StringBuffer reValue = new StringBuffer();
	   StringBuffer tem = new StringBuffer();
	   for (int i = 0; i < bytes.length; i++)
	   {
	    tem.delete(0, tem.length());
	    tem.append(Integer.toHexString(bytes[i] & 0xFF));
	    if (tem.length() == 1)
	    {
	     tem.insert(0, '0');
	    }
	    reValue.append(tem);
	   }
	   return reValue.toString().toUpperCase();
	}


}
